<?php

namespace App\Http\Controllers\Backend\Change;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Backend\BaseController;
use App\Repository\Repositories\Interfaces\ChangeRepository;
use Illuminate\Http\Request;
use Prettus\Repository\Criteria\RequestCriteria;

/**
 * 退货管理控制器
 *
 * @package App\Http\Controllers\Backend
 */
class ChangeController extends BaseController
{
	/**
	 * @var ChangeRepository
	 */
	protected $change_repo;


	public function __construct(ChangeRepository $change_repo, Request $request)
	{
		parent::__construct();
		$this->change_repo = $change_repo;
	}

	public function index()
	{
		$change = $this->change_repo->paginate($this->perPage);
		foreach ($change['data'] as $key =>$value){
			$change['data'][$key]['change_product'] = json_decode($value['change_product'],true);
		}
		return $this->success(ErrorConst::SUCCESS_CODE, $change, true);
	}

	public function show($id)
	{
		$change= $this->change_repo->find($id);
		return $this->success(ErrorConst::SUCCESS_CODE, $change['data']);
	}

	public function update(Request $request, $id)
	{
		$this->validate($request, [
			'status' => 'required'
		]);
		$data = $request->all();

		$ret = $this->change_repo->update($data, $id);
		if ($ret) {
			return $this->success(ErrorConst::SUCCESS_CODE, ErrorConst::SUCCESS_CODE_MSG);
		} else {
			return $this->error(ErrorConst::FAILED_CODE);
		}
	}

	public function delete($id)
	{
		//删除现在没有，不实现
//		$ret = $this->change_repo->delete($id);
//
//		if ($ret) {
			return $this->success(ErrorConst::SUCCESS_CODE, ErrorConst::SUCCESS_CODE_MSG);
//		} else {
//			return $this->error(ErrorConst::FAILED_CODE);
//		}
	}
}
